package com.example.service.impl;

import ch.qos.logback.classic.Logger;
import com.example.entity.authority.SysRoleAuthor;
import com.example.entity.authority.SystemAuthor;
import com.example.ext.entity.ExtSystemAuthor;
import com.example.mapp.SysRoleAuthorMapp;
import com.example.mapp.SystemAuthorMapp;
import com.example.service.SystemAuthorService;
import com.example.util.ResultUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by youkun on 2017/9/2.
 */
@Service
public class SystemAuthorServiceImpl  implements SystemAuthorService{

    private final static Logger logger = (Logger) LoggerFactory.getLogger(SystemAuthorServiceImpl.class);

    @Autowired
    private SystemAuthorMapp systemAuthorMapp;

    @Autowired
    private SysRoleAuthorMapp sysRoleAuthorMapp;

    /**
     * 查询
     * @param extSystemAuthor
     * @return
     */
    @Override
    public Map<String,Object> querySystemAuthorList(ExtSystemAuthor extSystemAuthor) {
        List<ExtSystemAuthor> list =null;
        Map<String,Object> map = new HashMap<String,Object>();
        try {
            Page<ExtSystemAuthor> page = PageHelper.startPage(extSystemAuthor.getCurrentPage(), extSystemAuthor.getPageSize());
            list = systemAuthorMapp.querySystemAuthorList(extSystemAuthor);
            map.put("list",list);
            map.put("total",page.getTotal());

        } catch (Exception e) {
            logger.error(e.getMessage());
            return ResultUtil.returnError("查询失败"+e.getMessage(),null);
        }
        return ResultUtil.returnSuccess("查询成功",map);
    }

    /**
     * 添加
     * @param systemAuthor
     * @return
     */
    @Override
    public Map<String, Object> addSystemAuthor(SystemAuthor systemAuthor) {
        try {
            ExtSystemAuthor sys = systemAuthorMapp.querySystemAuthorByName(systemAuthor.getAuthorName());
            if(null==sys){
                systemAuthorMapp.addSystemAuthor(systemAuthor);
                //零时添加的管理员添加添加到管理员角色下
                SysRoleAuthor sysRoleAuthor = new SysRoleAuthor();
                sysRoleAuthor.setRoleId(1);
                sysRoleAuthor.setAuthorId(Integer.parseInt(systemAuthor.getId().toString()));
                sysRoleAuthorMapp.add(sysRoleAuthor);
            }else {
                throw new Exception(systemAuthor.getAuthorName()+"已存在!");
            }

        }catch (Exception e){
            logger.error(e.getMessage());
            return ResultUtil.returnError(e.getMessage(),null);
        }
        return ResultUtil.returnSuccess("成功",null);
    }

    /**
     * 更新
     * @param systemAuthor
     * @return
     */
    @Override
    public Map<String, Object> updateSystemAuthor(SystemAuthor systemAuthor) {
        try {
            //查询数据是否存在
            ExtSystemAuthor system = systemAuthorMapp.querySystemAuthorById(systemAuthor.getId().toString());
            if(null==system){
                throw new Exception("数据不存在!");
            }
            if(systemAuthor.getAuthorName().equals(system.getAuthorName())){
                systemAuthorMapp.updateSystemAuthor(systemAuthor);
            }else{
                ExtSystemAuthor sys = systemAuthorMapp.querySystemAuthorByName(systemAuthor.getAuthorName());
                if(null==sys){
                    systemAuthorMapp.updateSystemAuthor(systemAuthor);
                }else {
                    throw new Exception(systemAuthor.getAuthorName()+"已存在!");
                }
            }
        }catch (Exception e){
            logger.error(e.getMessage());
            return ResultUtil.returnError(e.getMessage(),null);
        }
        return ResultUtil.returnSuccess("成功",null);
    }

    /**
     * 查询详情
     * @param id
     * @return
     */
    @Override
    public Map<String, Object> querySystemAuthorById(String id) {
        ExtSystemAuthor systemAuthor = null;
        try {
            systemAuthor = systemAuthorMapp.querySystemAuthorById(id);
        }catch (Exception e){
            logger.error(e.getMessage());
            return ResultUtil.returnError(e.getMessage(),null);
        }
        return ResultUtil.returnSuccess("成功",systemAuthor);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @Override
    @Transactional
    public Map<String, Object> deleteSystemAuthorById(String id) {
        try {
            int i = systemAuthorMapp.deleteSystemAuthorById(id);
            int d = sysRoleAuthorMapp.deleteSysRoleAuthorByAuthorId(id);
        }catch (Exception e){
            //手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            logger.error(e.getMessage());
            return ResultUtil.returnError(e.getMessage(),null);
        }
        return ResultUtil.returnSuccess("成功",null);
    }

}
